package com.ken.android.music.view.fragment;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.ken.android.music.model.ListInfo;
import com.ken.android.music.model.MusicBean;
import com.ken.android.music.mvp.B.BasePresenter;
import com.ken.android.music.service.PlayerService;
import com.ken.android.music.R;
import com.ken.android.music.service.ComponentUtil;
import com.ken.android.music.view.activity.MusicPlayingActivity;
import com.ken.android.music.view.adapter.MyAdapter;
import com.ken.android.music.view.presenter.ListFragmentContract;
import com.ken.android.music.view.presenter.ListFragmentPresenter;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;


/**
 * Created by axnshy on 16/5/21.
 */
public class ListFragment extends BaseAudioFragment<ListFragmentContract.View,ListFragmentPresenter> implements ListFragmentContract.View {
    private List<MusicBean> data = new ArrayList<>();
    private MyAdapter mAdapter;
    private RecyclerView mListView;
    private ListInfo listInfo;
    OnItemClickListener mListener;
    TextView listCountTx;
    TextView listNameTx;
    TextView listCreatorTx;
    ImageView listAvatarImg;
    ImageView listCreatorAvatarImg;

    @Override
    public ListFragmentPresenter createPresenter() {
        return new ListFragmentPresenter();
    }

    @Override
    public int onSetContent() {
        return R.layout.list_fragment;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listCountTx= (TextView) view.findViewById(R.id.tv_list_count);
        listCreatorTx= (TextView) view.findViewById(R.id.tv_list_creator_name);
        listNameTx= (TextView) view.findViewById(R.id.tv_list_name);
        listAvatarImg= (ImageView) view.findViewById(R.id.iv_list_avator);
        listCreatorAvatarImg= (ImageView) view.findViewById(R.id.iv_list_creator_avatar);
        mListView = view.findViewById(R.id.lv_fragment_musicList);
        listInfo = getArguments().getParcelable("listinfo");
        initView();
        initList();
    }

    private void initList() {
        // mList=new ArrayList<>();
        //        mList = MusicDao.getMusicList(view.getContext(), ListsList.get(listPositon).getListId());

        mAdapter = new MyAdapter(getContext(), data);
        mAdapter.setListener(new MyAdapter.OnItemListener() {
            @Override
            public void onItemMenuClick(MusicBean bean) {

            }

            @Override
            public void onItemClick() {

            }
        });
        System.out.println("mAdapter" + mAdapter);
        mListView.setAdapter(mAdapter);
    }

    private void initView() {
        if(listInfo.getBackgroundPath()!=null) {
            listAvatarImg.setImageDrawable(Drawable.createFromPath(listInfo.getBackgroundPath()));
        }
        else
        {
            listAvatarImg.setImageResource(R.drawable.h1);
        }
        listCountTx.setText(listInfo.getListCount()+"");
        listNameTx.setText(listInfo.getListName());
        listCreatorTx.setText(listInfo.getUserId()+"");
    }

    /*
    * 接口回调,在父Activity中实现该方法,在fragment中想要回调的地方调用mLister的方法
    * */
    public interface OnItemClickListener {
        public void updateToolbar(String string);
    }
}
